<template>
  <basic-container>
    <span class="primary my-2 w-full text-center" style="font-size: 14px; display: inline-block">
            <span class="danger">*</span>
            该界面功能：对山东省内IP主动攻击其他地区进行统计，记录首次发起攻击时间、最新攻击时间、该IP归属地区。
    </span>
    <el-form :inline="true" :model="query" class="mx-2" size="small">
      <el-row>
          <el-col :span="4">
              <el-form-item label="发起攻击IP区域">
                  <el-switch
                          @change="changeAttackerProvince"
                          style="margin-bottom: 3px"
                          :size="size"
                          v-model="query.onlySd"
                          active-text="仅山东"
                          inactive-text="全部">
                  </el-switch>
              </el-form-item>
          </el-col>
          <el-col :span="8">
              <el-form-item label="发起攻击IP国家/省/市">
                  <country-province-city-cascader v-model="query.countryProvinceCity"></country-province-city-cascader>
              </el-form-item>
          </el-col>
          <el-col :span="6">
              <el-form-item>
                  <el-button type="primary" icon="el-icon-search" @click="searchChange">搜 索</el-button>
              </el-form-item>
          </el-col>
      </el-row>
    </el-form>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               v-model="form"
               ref="crud"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
        <template slot="menuLeft">
            <excel-export v-bind="excelExportProps"></excel-export>
        </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList} from "@/youan/api/sd_attack_ip_detail/sdAttackIpDetail";
  import excelExport from "@/youan/components/excel/excel-export";
  import CountryProvinceCityCascader from "../../../components/form/country-province-city-cascader";

  export default {
    components: {CountryProvinceCityCascader, excelExport},
    data() {
      return {
        form: {},
        query: {
          onlySd: false,
          countryProvinceCity: [],
          ips: ""
        },
        loading: true,
        excelExportProps: {
          exportUrl: "/api/sd_attack_ip_detail/exportAttackIpDetailExcel",
          prepareQuery: this.prepareQuery,
          loadingText: "",
          excelName: "发起攻击IP时间信息"
        },
        page: {
          pageSize: 50,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          dialogClickModal: false,
          menu: false,
          addBtn: false,
          column: [
            {
              label: "",
              prop: "id",
              hide: true
            },
            {
              label: "发起攻击IP",
              prop: "attackerIp",
            },
            {
              label: "发起攻击国家",
              prop: "attackerCountry",
            },
            {
              label: "发起攻击省份",
              prop: "attackerProvince",
            },
            {
              label: "发起攻击城市",
              prop: "attackerCity",
            },
            {
              label: "首次攻击时间",
              prop: "firstAttackTime",
            },
            {
              label: "最近一次攻击时间",
              prop: "latestAttackTime",
            },
            {
              label: "攻击成功次数",
              prop: "attackSuccessNum",
            },
          ]
        },
        data: []
      };
    },
    methods: {
      searchChange() {
        this.page.currentPage = 1;
        this.onLoad(this.page);
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        if(this.query.countryProvinceCity) {
          params.attackerCountry = this.query.countryProvinceCity[0] || "";
          params.attackerProvince = this.query.countryProvinceCity[1] || "";
          params.attackerCity = this.query.countryProvinceCity[2] || "";
        }
        if(this.query.onlySd) {
          params.attackerProvince = "山东";
        }
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        });
      },
      changeAttackerProvince() {
        this.query.countryProvinceCity = [];
        this.onLoad(this.page);
      },
      prepareQuery() {
        let params = {};
        if(this.query.onlySd) {
          params.attackerProvince = "山东";
        }
        if(this.query.countryProvinceCity) {
          params.attackerCountry = this.query.countryProvinceCity[0] || "";
          params.attackerProvince = this.query.countryProvinceCity[1] || "";
          params.attackerCity = this.query.countryProvinceCity[2] || "";
        }
        return params;
      }
    },
    activated() {
      this.$nextTick(() => {
        if (this.$refs.crud.$refs.table) {
          this.$refs.crud.$refs.table.doLayout();
        }
      })
    },
  };
</script>

<style>
</style>
